package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Autowired
    private OrderFeignClient orderFeignClient;
    @Autowired
    private RabbitService rabbitService;


    //生成支付表
    @Override
    public PaymentInfo insertPaymentInfo(Long orderId, PaymentType paymentType) {
        //防止 幂等性问题
        //1 查询 看看是否已经有此订单的支付表了
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("order_id", orderId));
        if (paymentInfo == null){
            //1.1 没有生成支付表
            paymentInfo = new PaymentInfo();
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
            paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
            paymentInfo.setOrderId(orderInfo.getId());
            paymentInfo.setPaymentType(paymentType.name());
            //银行流水 支付成功之后银行才会产生流水 暂时没有
            paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
            paymentInfo.setSubject(orderInfo.getTradeBody());
            paymentInfo.setPaymentStatus(PaymentStatus.UNPAID.name());
            paymentInfo.setCreateTime(new Date());
            //回调时间 回调信息 暂时没有
            paymentInfoMapper.insert(paymentInfo);
        }else {
            //1.2 有 更新支付表 支付方式 （支付宝 微信）时间
            paymentInfo.setPaymentType(paymentType.name());
            paymentInfo.setCreateTime(new Date());
            paymentInfoMapper.updateById(paymentInfo);
        }

        return paymentInfo;
    }
    //更新支付表
    @Override
    public void update(Map<String, String> paramsMap) {
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
                .eq("out_trade_no", paramsMap.get("out_trade_no")));
        if (paymentInfo != null && PaymentStatus.UNPAID.name().equals(paymentInfo.getPaymentStatus())){
            //支付中 改已支付
            paymentInfo.setPaymentStatus(PaymentStatus.PAID.name());
            paymentInfo.setTradeNo(paramsMap.get("trade_no"));
            try {
                paymentInfo.setCallbackTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                .parse(paramsMap.get("notify_time")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            paymentInfo.setCallbackContent(JSONObject.toJSONString(paramsMap));
            paymentInfoMapper.updateById(paymentInfo);
            //发消息  保存最终一致性 事务问题 更新订单表
            rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_PAYMENT_PAY,
                    MqConst.ROUTING_PAYMENT_PAY,paymentInfo.getOrderId());

        }
    }

    @Override
    public void update(PaymentInfo paymentInfo) {
        paymentInfoMapper.updateById(paymentInfo);
    }

    @Override
    public PaymentInfo getPaymentInfo(String out_trade_no, String name) {
        return paymentInfoMapper.selectOne(new QueryWrapper<PaymentInfo>()
        .eq("out_trade_no",out_trade_no)
        .eq("payment_type",name));
    }

}
